import axios from 'axios'

const api = axios.create({
  baseURL: import.meta.env.VITE_API_URL || 'http://localhost:8000/api/v1'
})

// 请求拦截器添加 token
api.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
})

// 导出所有API函数
export const login = async (email, password) => {
  const response = await api.post('/auth/token', {
    username: email,
    password: password
  }, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
  return response.data
}

export const register = async (email, password) => {
  const response = await api.post('/auth/register', {
    email,
    password
  })
  return response.data
}

export const shortenUrl = async (url, expiresAt = null) => {
  const response = await api.post('/urls/shorten', { 
    original_url: url,
    expires_at: expiresAt 
  })
  return response.data
}

export const getUrlStats = async (shortCode) => {
  const response = await api.get(`/analytics/stats/${shortCode}`)
  return response.data
}

export const getUrlList = async () => {
  const response = await api.get('/urls')
  return response.data
} 